package main

import (
	"flag"
	"fmt"
	"log"
	"m3u8d/task"
	"net/url"
	"os"
	"time"
)

func main() {
	log.SetFlags(log.Lshortfile | log.LstdFlags)
	fp, err := os.OpenFile(fmt.Sprintf("%s.log", time.Now().Format("2006-01-02")), os.O_CREATE|os.O_APPEND, os.ModePerm)
	if err != nil {
		fmt.Println(err)
		return
	}
	log.SetOutput(fp)

	srcUrl := flag.String("u", "", "the m3u8 url")
	outputFileName := flag.String("o", "", "the output file")
	if len(os.Args) == 2 {
		if _, err := url.Parse(os.Args[1]); err == nil {
			srcUrl = &os.Args[1]
		} else {
			fmt.Println(err)
			return
		}
	} else {
		flag.Parse()
		if len(*srcUrl) == 0 {
			fmt.Println("please input the url with -u")
			return
		}
	}

	log.Printf("-->下载任务：%s", *srcUrl)
	if err := task.DownloadItems(*srcUrl, *outputFileName); err != nil {
		log.Println("下载任务执行失败：", err)
	}
}
